#include "choosecitydialog.h"
#include <QApplication>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include "ui_choosecitydialog.h"

QMap<QString, QString> ChooseCityDialog::cityMapTranslations = {
    // A to F
    {"中国澳门", {"Macau"}},
    {"阿里", {"Ali"}},
    {"安康", {"Ankang"}},
    {"阿克苏", {"Akesu"}},
    {"阿勒泰", {"Altay"}},
    {"安庆", {"Anqing"}},
    {"鞍山", {"Anshan"}},
    {"安阳", {"Anyang"}},
    {"阿尔山", {"Aershan"}},
    {"阿拉尔", {"Alar"}},
    {"阿拉善左", {"Alashan Left"}},
    {"阿拉善右", {"Alashan Right"}},
    {"北京", {"Beijing"}},
    {"包头", {"Baotou"}},
    {"北海", {"Beihai"}},
    {"保山", {"Baoshan"}},
    {"白山", {"Baishan"}},
    {"白城", {"Baicheng"}},
    {"百色", {"Baise"}},
    {"博乐", {"Bole"}},
    {"布尔津", {"Buerjin"}},
    {"巴彦淖尔", {"Bayannur"}},
    {"巴中", {"Bazhong"}},
    {"毕节", {"Bijie"}},
    {"重庆", {"Chongqing"}},
    {"成都", {"Chengdu"}},
    {"长治", {"Changzhi"}},
    {"长春", {"Changchun"}},
    {"常德", {"Changde"}},
    {"赤峰", {"Chifeng"}},
    {"长沙", {"Changsha"}},
    {"朝阳", {"Chaoyang"}},
    {"常州", {"Changzhou"}},
    {"池州", {"Chizhou"}},
    {"承德", {"Chengde"}},
    {"昌都", {"Changdu"}},
    {"郴州", {"Chenzhou"}},
    {"沧源", {"Cangyuan"}},
    {"大连", {"Dalian"}},
    {"敦煌", {"Dunhuang"}},
    {"大理", {"Dali"}},
    {"迪庆", {"Diqing"}},
    {"大同", {"Datong"}},
    {"丹东", {"Dandong"}},
    {"大庆", {"Daqing"}},
    {"东营", {"Dongying"}},
    {"稻城", {"Daocheng"}},
    {"达州", {"Dazhou"}},
    {"德令哈", {"Delingha"}},
    {"恩施", {"Enshi"}},
    {"鄂州", {"Ezhou"}},
    {"鄂尔多斯", {"Ordos"}},
    {"额济纳旗", {"Ejin Banner"}},
    {"二连浩特", {"Erenhot"}},
    {"佛山", {"Foshan"}},
    {"富蕴", {"Fuyun"}},
    {"阜阳", {"Fuyang"}},
    {"福州", {"Fuzhou"}},
    {"抚远", {"Fuyuan"}},
    {"广州", {"Guangzhou"}},
    {"桂林", {"Guilin"}},
    {"贵阳", {"Guiyang"}},
    {"格尔木", {"Golmud"}},
    {"广元", {"Guangyuan"}},
    {"赣州", {"Ganzhou"}},
    {"固原", {"Guyuan"}},
    {"中国高雄", {"Kaohsiung"}},
    {"甘孜", {"Ganzi"}},
    {"果洛", {"Guoluo"}},

    // G to M
    {"哈尔滨", {"Harbin"}},
    {"杭州", {"Hangzhou"}},
    {"黄山", {"Huangshan"}},
    {"海口", {"Haikou"}},
    {"呼和浩特", {"Hohhot"}},
    {"汉中", {"Hanzhong"}},
    {"海拉尔", {"Hailaer"}},
    {"邯郸", {"Handan"}},
    {"合肥", {"Hefei"}},
    {"黑河", {"Heihe"}},
    {"怀化", {"Huaihua"}},
    {"哈密", {"Hami"}},
    {"惠州", {"Huizhou"}},
    {"淮安", {"Huai'an"}},
    {"荆泽", {"Jingze"}},
    {"花土沟", {"Huatugou"}},
    {"和静", {"Hejing"}},
    {"和田", {"Hotan"}},
    {"衡阳", {"Hengyang"}},
    {"河池", {"Hechi"}},
    {"霍林郭勒", {"Huolinguole"}},
    {"中国花莲", {"Hualien"}},
    {"红原", {"Hongyuan"}},
    {"九江", {"Jiujiang"}},
    {"九寨沟", {"Jiuzhaigou"}},
    {"济南", {"Jinan"}},
    {"鸡西", {"Jixi"}},
    {"景德镇", {"Jingdezhen"}},
    {"井冈山", {"Jinggangshan"}},
    {"佳木斯", {"Jiamusi"}},
    {"济宁", {"Jining"}},
    {"嘉峪关", {"Jiayuguan"}},
    {"锦州", {"Jinzhou"}},
    {"荆州", {"Jingzhou"}},
    {"揭阳", {"Jieyang"}},
    {"建三江", {"Jiansanjiang"}},
    {"昆明", {"Kunming"}},
    {"喀什", {"Kashgar"}},
    {"克拉玛依", {"Karamay"}},
    {"库车", {"Kuqa"}},
    {"库尔勒", {"Korla"}},
    {"凯里", {"Kaili"}},
    {"康定", {"Kangding"}},
    {"丽江", {"Lijiang"}},
    {"拉萨", {"Lhasa"}},
    {"兰州", {"Lanzhou"}},
    {"林芝", {"Nyingchi"}},
    {"临汾", {"Linfen"}},
    {"连云港", {"Lianyungang"}},
    {"柳州", {"Liuzhou"}},
    {"泸州", {"Luzhou"}},
    {"临沂", {"Linyi"}},
    {"六盘水", {"Liupanshui"}},
    {"临洮", {"Lintao"}},
    {"荔波", {"Libo"}},
    {"阆中", {"Langzhong"}},
    {"黎平", {"Liping"}},
    {"潞州", {"Luzhou"}},
    {"吕梁", {"Lvliang"}},
    {"陇南", {"Longnan"}},
    {"龙岩", {"Longyan"}},
    {"牡丹江", {"Mudanjiang"}},
    {"漠河", {"Mohe"}},
    {"绵阳", {"Mianyang"}},
    {"满洲里", {"Manzhouli"}},
    {"茂台", {"Maotai"}},
    {"梅州", {"Meizhou"}},
    {"芒市", {"Mangshi"}},
    {"中国马公", {"Magong"}},
    {"中国马祖", {"Matsu"}},
    {"南京", {"Nanjing"}},
    {"南通", {"Nantong"}},
    {"宁波", {"Ningbo"}},
    {"南昌", {"Nanchang"}},
    {"南充", {"Nanchong"}},
    {"南宁", {"Nanning"}},
    {"南阳", {"Nanyang"}},
    {"宁蒗", {"Ninglang"}},
    {"中国南竿", {"Nangan"}},

    // N to W
    {"攀枝花", {"Panzhihua"}},
    {"普洱", {"Pu'er"}},
    {"青岛", {"Qingdao"}},
    {"琼海", {"Qionghai"}},
    {"秦皇岛", {"Qinhuangdao"}},
    {"齐齐哈尔", {"Qiqihar"}},
    {"且末", {"Qiemo"}},
    {"庆阳", {"Qingyang"}},
    {"泉州", {"Quanzhou"}},
    {"衢州", {"Quzhou"}},
    {"祁连", {"Qilian"}},
    {"奇台", {"Qitai"}},
    {"黔江", {"Qianjiang"}},
    {"日喀则", {"Shigatse"}},
    {"日照", {"Rizhao"}},
    {"若羌", {"Ruoqiang"}},
    {"上海", {"Shanghai"}},
    {"深圳", {"Shenzhen"}},
    {"三亚", {"Sanya"}},
    {"上饶", {"Shangrao"}},
    {"韶关", {"Shaoguan"}},
    {"石河子", {"Shihezi"}},
    {"石家庄", {"Shijiazhuang"}},
    {"三明", {"Sanming"}},
    {"山南", {"Shannan"}},
    {"沈阳", {"Shenyang"}},
    {"十堰", {"Shiyan"}},
    {"神农架", {"Shennongjia"}},
    {"邵阳", {"Shaoyang"}},
    {"松原", {"Songyuan"}},
    {"朔州", {"Shuozhou"}},
    {"萨车", {"Sa'er"}},
    {"泉州", {"Quanzhou"}},
    {"天津", {"Tianjin"}},
    {"太原", {"Taiyuan"}},
    {"塔城", {"Tacheng"}},
    {"通化", {"Tonghua"}},
    {"通辽", {"Tongliao"}},
    {"天水", {"Tianshui"}},
    {"唐山", {"Tangshan"}},
    {"台州", {"Taizhou"}},
    {"中国台北", {"Taipei"}},
    {"铜仁", {"Tongren"}},
    {"腾冲", {"Tengchong"}},
    {"中国台南", {"Tainan"}},
    {"中国台东", {"Taitung"}},
    {"中国台中", {"Taichung"}},
    {"塔什库尔干", {"Tashkurgan"}},
    {"吐鲁番", {"Turpan"}},
    {"图木舒克", {"Tumushuke"}},
    {"汤阴", {"Tangyin"}},
    {"无锡", {"Wuxi"}},
    {"武夷山", {"Wuyishan"}},
    {"乌鲁木齐", {"Urumqi"}},
    {"五大连池", {"Wudalianchi"}},
    {"潍坊", {"Weifang"}},
    {"武汉", {"Wuhan"}},
    {"芜湖", {"Wuhu"}},
    {"威海", {"Weihai"}},
    {"乌兰浩特", {"Ulanhot"}},
    {"万州", {"Wanzhou"}},
    {"温州", {"Wenzhou"}},
    {"梧州", {"Wuzhou"}},
    {"乌海", {"Uuhai"}},
    {"文山", {"Wenshan"}},
    {"乌拉特中", {"Urat Middle"}},
    {"武隆", {"Wulong"}},
    {"巫山", {"Wushan"}},
    {"乌兰察布", {"Ulanqab"}},

    // X to Z
    {"西安", {"Xi'an"}},
    {"厦门", {"Xiamen"}},
    {"西双版纳", {"Xishuangbanna"}},
    {"中国香港", {"Hong Kong"}},
    {"西宁", {"Xining"}},
    {"西昌", {"Xichang"}},
    {"襄阳", {"Xiangyang"}},
    {"夏河", {"Xiahe"}},
    {"锡林浩特", {"Xilinhot"}},
    {"信阳", {"Xinyang"}},
    {"徐州", {"Xuzhou"}},
    {"忻州", {"Xinzhou"}},
    {"邢台", {"Xingtai"}},
    {"兴义", {"Xingyi"}},
    {"新源", {"Xinyuan"}},
    {"湘西土家", {"Xiangxi Tujia"}},
    {"西安", {"Xi'an"}},
    {"迪庆", {"Diqing"}},
    {"扬州", {"Yangzhou"}},
    {"银川", {"Yinchuan"}},
    {"延安", {"Yan'an"}},
    {"运城", {"Yuncheng"}},
    {"宜宾", {"Yibin"}},
    {"宜昌", {"Yichang"}},
    {"伊春", {"Yichun"}},
    {"宜春", {"Yichun"}},
    {"延吉", {"Yanji"}},
    {"榆林", {"Yulin"}},
    {"伊宁", {"Yining"}},
    {"烟台", {"Yantai"}},
    {"义乌", {"Yiwu"}},
    {"岳阳", {"Yueyang"}},
    {"永州", {"Yongzhou"}},
    {"玉林", {"Yulin"}},
    {"盐城", {"Yancheng"}},
    {"营口", {"Yingkou"}},
    {"于田", {"Yutian"}},
    {"玉树", {"Yushu"}},
    {"舟山", {"Zhoushan"}},
    {"张家界", {"Zhangjiajie"}},
    {"珠海", {"Zhuhai"}},
    {"湛江", {"Zhanjiang"}},
    {"张家口", {"Zhangjiakou"}},
    {"昭通", {"Zhaotong"}},
    {"中卫", {"Zhongwei"}},
    {"遵义", {"Zunyi"}},
    {"郑州", {"Zhengzhou"}},
    {"张掖", {"Zhangye"}},
    {"扎兰屯", {"Zhalantun"}},
    {"昭苏", {"Zhaosu"}},
    {"怀化", {"Huaihua"}}};
// 反转 cityMapTranslations 以创建 englishToChineseMap
QMap<QString, QString> ChooseCityDialog::englishToChineseMap;

void ChooseCityDialog::initializeMaps()
{
    // 使用循环初始化englishToChineseMap
    for (auto it = cityMapTranslations.begin(); it != cityMapTranslations.end(); ++it) {
        englishToChineseMap[it.value()] = it.key();
    }
}

ChooseCityDialog::ChooseCityDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ChooseCityDialog)
{
    ui->setupUi(this);
    // 初始化映射
    initializeMaps();

    // 获取设计师中的 QGroupBox
    QGroupBox *cityAtoFBox = ui->CityAtoFbox;
    QGroupBox *cityGtoJBox = ui->CityGtoJbox;
    QGroupBox *cityKtoNBox = ui->CityKtoNbox;
    QGroupBox *cityPtoWBox = ui->CityPtoWbox;
    QGroupBox *cityXtoZBox = ui->CityXtoZbox;
    QGroupBox *cityAsiaBox = ui->CityAsiabox;
    QGroupBox *cityEuropeBox = ui->CityEuropebox;
    QGroupBox *cityAmerBox = ui->CityAmerbox;
    QGroupBox *cityAfiricaBox = ui->CityAfircabox;
    QGroupBox *cityOceanBox = ui->CityOceanbox;

    populateCityGrid(cityAtoFBox, cityMap1);
    populateCityGrid(cityGtoJBox, cityMap2);
    populateCityGrid(cityKtoNBox, cityMap3);
    populateCityGrid(cityPtoWBox, cityMap4);
    populateCityGrid(cityXtoZBox, cityMap5);
    populateCityGrid(cityAsiaBox, cityMap6);
    populateCityGrid(cityEuropeBox, cityMap7);
    populateCityGrid(cityAmerBox, cityMap8);
    populateCityGrid(cityAfiricaBox, cityMap9);
    populateCityGrid(cityOceanBox, cityMap10);
}

void ChooseCityDialog::populateCityGrid(QGroupBox *groupBox, QMap<QString, QStringList> cityMap)
{
    // 如果 QGroupBox 没有布局，设置一个 QGridLayout
    if (!groupBox->layout()) {
        QGridLayout *layout = new QGridLayout(groupBox);
        groupBox->setLayout(layout);
    }

    QGridLayout *gridLayout = qobject_cast<QGridLayout *>(groupBox->layout());

    int row = 0; // 行计数器
    for (const QString &key : cityMap.keys()) {
        // 添加首字符索引标签（第一列）
        QLabel *indexLabel = new QLabel(key, groupBox);
        gridLayout->addWidget(indexLabel, row, 0);

        // 添加城市按钮（从第二列开始）
        int col = 1;
        for (const QString &city : cityMap[key]) {
            QPushButton *cityButton = new QPushButton(city, groupBox);
            cityButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

            // 设置最小大小为按钮内容的大小
            cityButton->setMinimumSize(cityButton->sizeHint());

            // 连接按钮的点击信号到槽函数
            connect(cityButton, &QPushButton::clicked, this, &ChooseCityDialog::onCityButtonClicked);

            // 将按钮添加到布局中
            gridLayout->addWidget(cityButton, row, col);

            col++; // 移动到下一列

            // 如果列数超过 7，则换行
            if (col > 7) {
                col = 1; // 重置列为第一列
                row++;   // 增加行数
            }
        }

        // 每个首字符结束后换行
        row++;
    }
}

// 重载：处理 QStringList（不带 QMap）
void ChooseCityDialog::populateCityGrid(QGroupBox *groupBox, const QStringList &cityList)
{
    if (!groupBox->layout()) {
        QGridLayout *layout = new QGridLayout(groupBox);
        groupBox->setLayout(layout);
    }

    QGridLayout *gridLayout = qobject_cast<QGridLayout *>(groupBox->layout());

    int row = 0;
    int col = 0;
    for (const QString &city : cityList) {
        // 没有首字母索引的情况，直接添加城市按钮
        QPushButton *cityButton = new QPushButton(city, groupBox);
        cityButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        cityButton->setMinimumSize(cityButton->sizeHint());

        gridLayout->addWidget(cityButton, row, col);
        col++;

        if (col > 6) {
            col = 1;
            row++;
        }
    }
}

void ChooseCityDialog::onCityButtonClicked()
{
    // 获取触发信号的按钮
    QPushButton *button = qobject_cast<QPushButton *>(sender());
    if (button) {
        // 发出信号，将按钮的城市名称传递给外部窗口
        emit citySelected(button->text());
    }
}

ChooseCityDialog::~ChooseCityDialog()
{
    delete ui;
}
